//
//  LCMessage.m
//  TinyFortune
//
//  Created by lc on 15/1/6.
//  Copyright © 2016年 lc. All rights reserved.
//

#import "LCMessage.h"
#import "NSDate+Utils.h"

@implementation LCMessage

- (void)setWithDict:(NSDictionary *)dict{
    self.strIcon = dict[@"strIcon"];
    self.strName = dict[@"strName"];
    self.strId = dict[@"strId"];
    self.strTime = [self changeTheDateString:dict[@"strTime"]];
    self.from = [dict[@"from"] intValue];
    
    switch ([dict[@"type"] integerValue]) {
       //文本形式
        case 0:
            self.type = LCMessageTypeText ;
            self.strContent = dict[@"strContent"];
            break;
        case 1:
            self.type = LCMessageTypePicture;
            self.picture = dict[@"picture"];
            break ;
        case 2:
            self.type = LCMessageTypeVoice ;
            self.voice = dict[@"voice"];
            self.strVoiceTime = dict[@"strVoiceTime"];
        default:
            break;
    }
}
//"08-10 晚上08:09:41.0" ->"昨天 上午10:09"或者2012-08-10 凌晨07:09“
- (NSString *)changeTheDateString:(NSString *)str{
    NSString *subString = [str substringWithRange:NSMakeRange(0, 19)];
    NSDate *lastDate = [NSDate dateFromString:subString withFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate:lastDate];
    lastDate = [lastDate dateByAddingTimeInterval:interval];
    
    
    NSString *dateStr ; //年月日
    NSString *period ; //时间段
    NSString *hour ; //时
    
    if ([lastDate year]==[[NSDate date] year]) {
        NSInteger days = [NSDate daysOffsetBetweenStartDate:lastDate endDate:[NSDate date]];
        if (days <= 2) {
            dateStr = [lastDate stringYearMonthDayCompareToday];
        }else{
            dateStr = [lastDate stringMonthDay];
        }
    }else{
        dateStr = [lastDate stringYearMonthDay];
    }
    
    if ([lastDate hour] >= 5 && [lastDate hour] < 12) {
        period = @"早上";
        hour = [NSString stringWithFormat:@"%02d",(int)[lastDate hour]];
    }else if([lastDate hour]>=12 && [lastDate hour]<= 18){
        period = @"下午";
        hour = [NSString stringWithFormat:@"%02d",(int)[lastDate hour] - 12];
    }else if([lastDate hour ]> 18 && [lastDate hour]<=23){
        period = @"晚上" ;
        hour = [NSString stringWithFormat:@"%02d",(int)[lastDate hour]-12];
    }else{
        period = @"凌晨4";
        hour = [NSString stringWithFormat:@"%02d",(int)[lastDate hour]];
    }
    return [NSString stringWithFormat:@"%@ %@ %@:%02d",dateStr,period,hour,(int)[lastDate minute]];
}
- (void)minuteOffSetStart:(NSString *)start end:(NSString *)end{
    if (!start) {
        self.showDateLabel = YES ;
        return ;
    }
    NSString *subStart = [start substringWithRange:NSMakeRange(0, 19)];
    
    NSDate *starDate = [NSDate dateFromString:subStart withFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *subEnd = [end substringWithRange:NSMakeRange(0, 19)];
    NSDate *endDate = [NSDate dateFromString:subEnd withFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    //相隔的秒数
    NSTimeInterval timeInterval = [starDate timeIntervalSinceDate:endDate] ;
    if (fabs(timeInterval) > 5 * 60) {
        self.showDateLabel = YES ;
    }else{
        self.showDateLabel = NO ;
    }
    
    
    
    
}
@end
